Data Transformation

GVPT399F: Power, Politics, and Data

Learning objectives

  1. Learn basic operations in R

  2. Be introduced to dplyr

  3. Clean up and transform your data

R objects

Create new objects with <-

x <- 3 * 4

x
[1] 12


x <- 3 * 10

x
[1] 30

R functions

Many functions come with R straight out of the box:

seq(1, 10)
 [1]  1  2  3  4  5  6  7  8  9 10


You can create objects using functions:

x <- seq(1, 10)

x
 [1]  1  2  3  4  5  6  7  8  9 10

Gapminder

First, you need to install the gapminder package:

install.packages("gapminder")

Then access the gapminder data set:

library(tidyverse)
library(gapminder)

head(gapminder)
# A tibble: 6 × 6
  country     continent  year lifeExp      pop gdpPercap
  <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
1 Afghanistan Asia       1952    28.8  8425333      779.
2 Afghanistan Asia       1957    30.3  9240934      821.
3 Afghanistan Asia       1962    32.0 10267083      853.
4 Afghanistan Asia       1967    34.0 11537966      836.
5 Afghanistan Asia       1972    36.1 13079460      740.
6 Afghanistan Asia       1977    38.4 14880372      786.

Data types

In gapminder:

  • fctr stands for factors, which R uses to represent categorical variables with fixed possible values.

  • int stands for integer.

  • dbl stands for doubles (or real numbers).

Data types

Other types:

  • chr stands for character vectors, or strings.

  • dttm stands for date-times (a date + a time).

  • lgl stands for logical, vectors that contain only TRUE or FALSE.1

Introducing dplyr

Help you with most of your data transformation needs.

Five basic functions:

  • filter()

  • arrange()

  • select()

  • mutate()

  • summarise()